﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.NhanVienModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.NhanVien;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.NhanVienDelegate
{
    public class XeVanTaiDelegate
    {
        private NhanVienDataContext db = new NhanVienDataContext();
        int _UserId = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
        int _DeparmentId = Convert.ToInt32(HttpContext.Current.Session["DEPARTMENT"]);
        DateTime _DateNow = DateTime.Now;
        public IList<pt_XeVanTai> GetAllXeVanTai()
        {
            return db.pt_XeVanTais.Where(p => p.IsDelete == false).ToList();
        }
        public pt_XeVanTai Insert(pt_XeVanTai obj)
        {
            try
            {

                obj.CreateBy = _UserId;
                obj.CreateDate = _DateNow;
                db.pt_XeVanTais.InsertOnSubmit(obj);
                db.SubmitChanges();
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public bool DeleteXeVanTai(int ID)
        {
            try
            {
                pt_XeVanTai LuongNew =
                db.pt_XeVanTais.Single(p => p.Id == ID);
                db.pt_XeVanTais.DeleteOnSubmit(LuongNew);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool UpdateXeVanTai(pt_XeVanTai XeVanTai)
        {
            try
            {
                pt_XeVanTai XeVanTaiUpdate =
                    db.pt_XeVanTais.Single(p => p.Id == XeVanTai.Id);
                if (XeVanTaiUpdate != null && XeVanTaiUpdate.IsDelete == false)
                {
                    XeVanTaiUpdate.Id = XeVanTai.Id;
                    XeVanTaiUpdate.IdDangThungXe = XeVanTai.IdDangThungXe;
                    XeVanTaiUpdate.BangKiemSoat = XeVanTai.BangKiemSoat;
                    XeVanTaiUpdate.NamSanXuat = XeVanTai.NamSanXuat;
                    XeVanTaiUpdate.NamHanSuDung = XeVanTai.NamHanSuDung;
                    XeVanTaiUpdate.TrongTai = XeVanTai.TrongTai;
                    XeVanTaiUpdate.HanKiemDinhXe = XeVanTai.HanKiemDinhXe;
                    XeVanTaiUpdate.HetHanBaoHiem = XeVanTai.HetHanBaoHiem;
                    XeVanTaiUpdate.UpdateDate = XeVanTai.UpdateDate;
                    XeVanTaiUpdate.UpdateBy = XeVanTai.UpdateBy;
                    db.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception)
            {
                return false;
            }
        }


    
        public pt_XeVanTai GetXeVanTaiById(string id)
        {
            try
            {
                pt_XeVanTai item = this.db.pt_XeVanTais.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public pt_XeVanTai BuildnewXeVanTai(XeVanTaiForm form)
        {
            pt_XeVanTai newXeVanTai = new pt_XeVanTai();
            newXeVanTai.IdDangThungXe = form.IdDangThungXe;
            newXeVanTai.BangKiemSoat = form.BangKiemSoat;
            newXeVanTai.NamSanXuat = form.NamSanXuat;
            newXeVanTai.NamHanSuDung = form.NamHanSuDung;
            newXeVanTai.TrongTai = form.TrongTai;
            newXeVanTai.HanKiemDinhXe =  Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayDK);
            newXeVanTai.HetHanBaoHiem =  Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayBH);
            newXeVanTai.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newXeVanTai.CreateDate = DateTime.Now;
            newXeVanTai.IsDelete = false;
            return newXeVanTai;
        }


        public pt_XeVanTai BuildEditNhanVien(XeVanTaiForm form)
        {
            pt_XeVanTai newXeVanTai = new pt_XeVanTai();
            newXeVanTai.Id = form.Id;
            newXeVanTai.IdDangThungXe = form.IdDangThungXe;
            newXeVanTai.BangKiemSoat = form.BangKiemSoat;
            newXeVanTai.NamSanXuat = form.NamSanXuat;
            newXeVanTai.NamHanSuDung = form.NamHanSuDung;
            newXeVanTai.TrongTai = form.TrongTai;
            newXeVanTai.HanKiemDinhXe =  Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayDK);
            newXeVanTai.HetHanBaoHiem =  Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayBH);
            newXeVanTai.UpdateDate = DateTime.Now;
            newXeVanTai.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newXeVanTai;
        }


        public XeVanTaiForm ConvertToForm(pt_XeVanTai obj)
        {
            XeVanTaiForm form = new XeVanTaiForm();
            form.Id = obj.Id;
            form.IdDangThungXe = obj.IdDangThungXe;
            form.BangKiemSoat = obj.BangKiemSoat;
            form.NamSanXuat = obj.NamSanXuat;
            form.NamHanSuDung = obj.NamHanSuDung;
            form.TrongTai = obj.TrongTai;
            form.NgayDK = string.Format("{0:dd/MM/yyyy}",obj.HanKiemDinhXe);
            form.NgayBH = string.Format("{0:dd/MM/yyyy}",obj.HetHanBaoHiem);
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }

        public IQueryable<pt_XeVanTai> GetAll()
        {
            return db.pt_XeVanTais.Where(p => p.IsDelete == false).OrderByDescending(p => p.CreateBy);
        }
        public List<pt_XeVanTai> GetALLXeVanTai()
        {
            return db.pt_XeVanTais.Where(p => p.IsDelete == false).OrderByDescending(p => p.CreateBy).ToList();
        }

        public IQueryable<pt_XeVanTai> QuickSearch(int? pageNumberId, string SearchObject, string SearchInput)
        {
            IQueryable<pt_XeVanTai> lst = null;
            try
            {
                SearchInput = SearchInput.Trim();
                lst = this.GetAll();
                if (SearchObject.Equals("XeVanTai"))
                    lst = lst.Where(p => p.BangKiemSoat.ToString().Contains(SearchInput));
            }
            catch (Exception)
            {

            }
            return lst;
        }

        public SelectList BuildThungXe(int IdXeVanTai)
        {
            SelectList list = new SelectList(db.dm_DangThungXes.Where(p => p.IsDelete == false).ToList(), "Id", "TenThungXe", IdXeVanTai);
            return list;
        }
        public pt_XeVanTai ConvertToLinq(XeVanTaiForm form)
        {
            pt_XeVanTai obj = new pt_XeVanTai();
            obj.IdDangThungXe = form.IdDangThungXe;
            obj.BangKiemSoat = form.BangKiemSoat;
            obj.NamSanXuat = form.NamSanXuat;
            obj.NamHanSuDung = form.NamHanSuDung;
            obj.TrongTai = form.TrongTai;
            obj.HanKiemDinhXe = Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayDK);
            obj.HetHanBaoHiem = Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayBH);
         
            return obj;
        }
    }
}